<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">

<html><head><title>Auth - Bonfire</title><link rel="stylesheet" type="text/css" href="../../../../styles/main.css"><script language=JavaScript src="../../../../javascript/main.js"></script><script language=JavaScript src="../../../../javascript/prettify.js"></script><script language=JavaScript src="../../../../javascript/searchdata.js"></script></head><body class="ContentPage" onLoad="NDOnLoad();prettyPrint();"><script language=JavaScript><!--
if (browserType) {document.write("<div class=" + browserType + ">");if (browserVer) {document.write("<div class=" + browserVer + ">"); }}// --></script>

<!--  Generated by Natural Docs, version 1.5 -->
<!--  http://www.naturaldocs.org  -->

<!-- saved from url=(0026)http://www.naturaldocs.org -->




<div id=Content><div class="CClass"><div class=CTopic id=MainTopic><h1 class=CTitle><a name="Auth"></a>Auth</h1><div class=CBody><p>Provides authentication functions for logging users in/out, restricting access to controllers, and managing login attempts.</p><p>Security and ease-of-use are the two primary goals of the Auth system in Bonfire.&nbsp; This lib will be constantly updated to reflect the latest security practices that we learn about, while maintaining the simple API.</p><h4 class=CHeading>Author</h4><p>Lonnie Ezell</p><!--START_ND_SUMMARY--><div class=Summary><div class=STitle>Summary</div><div class=SBorder><table border=0 cellspacing=0 cellpadding=0 class=STable><tr class="SMain"><td class=SEntry><a href="#Auth" >Auth</a></td><td class=SDescription>Provides authentication functions for logging users in/out, restricting access to controllers, and managing login attempts.</td></tr><tr class="SGroup SIndent1"><td class=SEntry><a href="#Auth.Variables" >Variables</a></td><td class=SDescription></td></tr><tr class="SVariable SIndent2 SMarked"><td class=SEntry><a href="#Auth.$errors" id=link1 onMouseOver="ShowTip(event, 'tt1', 'link1')" onMouseOut="HideTip('tt1')">$errors</a></td><td class=SDescription>An array of errors generated.</td></tr><tr class="SVariable SIndent2"><td class=SEntry><a href="#Auth.$logged_in" id=link2 onMouseOver="ShowTip(event, 'tt2', 'link2')" onMouseOut="HideTip('tt2')">$logged_in</a></td><td class=SDescription>Stores the logged in value after the first test to improve performance.</td></tr><tr class="SVariable SIndent2 SMarked"><td class=SEntry><a href="#Auth.$ip_address" id=link3 onMouseOver="ShowTip(event, 'tt3', 'link3')" onMouseOut="HideTip('tt3')">$ip_address</a></td><td class=SDescription>Stores the ip_address of the current user for performance reasons.</td></tr><tr class="SVariable SIndent2"><td class=SEntry><a href="#Auth.$perms" id=link4 onMouseOver="ShowTip(event, 'tt4', 'link4')" onMouseOut="HideTip('tt4')">$perms</a></td><td class=SDescription>Stores permissions by role so we don&rsquo;t have to scour the database more than once.</td></tr><tr class="SVariable SIndent2 SMarked"><td class=SEntry><a href="#Auth.$ci" id=link5 onMouseOver="ShowTip(event, 'tt5', 'link5')" onMouseOut="HideTip('tt5')">$ci</a></td><td class=SDescription>A pointer to the CodeIgniter instance.</td></tr><tr class="SGroup SIndent1"><td class=SEntry><a href="#Auth.Functions" >Functions</a></td><td class=SDescription></td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.__construct" id=link6 onMouseOver="ShowTip(event, 'tt6', 'link6')" onMouseOut="HideTip('tt6')">__construct()</a></td><td class=SDescription>Grabs a pointer to the CI instance, gets the user&rsquo;s IP address, and attempts to automatically log in the user.</td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.login" id=link7 onMouseOver="ShowTip(event, 'tt7', 'link7')" onMouseOut="HideTip('tt7')">login()</a></td><td class=SDescription>Attempt to log the user in.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.logout" id=link8 onMouseOver="ShowTip(event, 'tt8', 'link8')" onMouseOut="HideTip('tt8')">logout()</a></td><td class=SDescription>Destroys the autologin information and the current session.</td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.is_logged_in" id=link9 onMouseOver="ShowTip(event, 'tt9', 'link9')" onMouseOut="HideTip('tt9')">is_logged_in()</a></td><td class=SDescription>Checks the session for the required info, then verifies against the database.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.restrict" id=link10 onMouseOver="ShowTip(event, 'tt10', 'link10')" onMouseOut="HideTip('tt10')">restrict()</a></td><td class=SDescription>Checks that a user is logged in (and, optionally of the correct role) and, if not, send them to the login screen.</td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.user_id" id=link11 onMouseOver="ShowTip(event, 'tt11', 'link11')" onMouseOut="HideTip('tt11')">user_id()</a></td><td class=SDescription>Retrieves the user_id from the current session.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.email" id=link12 onMouseOver="ShowTip(event, 'tt12', 'link12')" onMouseOut="HideTip('tt12')">email()</a></td><td class=SDescription>Retrieves the email address from the current session.</td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.role_id" id=link13 onMouseOver="ShowTip(event, 'tt13', 'link13')" onMouseOut="HideTip('tt13')">role_id()</a></td><td class=SDescription>Retrieves the role_id from the current session.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.has_permission" id=link14 onMouseOver="ShowTip(event, 'tt14', 'link14')" onMouseOut="HideTip('tt14')">has_permission()</a></td><td class=SDescription>Verifies that the user is logged in and has the appropriate access permissions.</td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.role_name_by_id" id=link15 onMouseOver="ShowTip(event, 'tt15', 'link15')" onMouseOut="HideTip('tt15')">role_name_by_id()</a></td><td class=SDescription>Retrieves the role_name for the request role.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.increase_login_attempts" id=link16 onMouseOver="ShowTip(event, 'tt16', 'link16')" onMouseOut="HideTip('tt16')">increase_login_attempts()</a></td><td class=SDescription>Records a login attempt into the database.</td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.clear_login_attempts" id=link17 onMouseOver="ShowTip(event, 'tt17', 'link17')" onMouseOut="HideTip('tt17')">clear_login_attempts()</a></td><td class=SDescription>Clears all login attempts for this user, as well as cleans out old logins.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.num_login_attempts" id=link18 onMouseOver="ShowTip(event, 'tt18', 'link18')" onMouseOut="HideTip('tt18')">num_login_attempts()</a></td><td class=SDescription>Get number of attempts to login occured from given IP-address and/or login</td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.autologin" id=link19 onMouseOver="ShowTip(event, 'tt19', 'link19')" onMouseOut="HideTip('tt19')">autologin()</a></td><td class=SDescription>Attempts to log the user in based on an existing &lsquo;autologin&rsquo; cookie.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.create_autologin" id=link20 onMouseOver="ShowTip(event, 'tt20', 'link20')" onMouseOut="HideTip('tt20')">create_autologin()</a></td><td class=SDescription>Create the auto-login entry in the database. </td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.delete_autologin" id=link21 onMouseOver="ShowTip(event, 'tt21', 'link21')" onMouseOut="HideTip('tt21')">delete_autologin()</a></td><td class=SDescription>Deletes the autologin cookie for the current user.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.setup_session" id=link22 onMouseOver="ShowTip(event, 'tt22', 'link22')" onMouseOut="HideTip('tt22')">setup_session()</a></td><td class=SDescription>Creates the session information for the current user. </td></tr><tr class="SFunction SIndent2"><td class=SEntry><a href="#Auth.auth_errors" id=link23 onMouseOver="ShowTip(event, 'tt23', 'link23')" onMouseOut="HideTip('tt23')">auth_errors()</a></td><td class=SDescription>A utility function for showing authentication errors.</td></tr><tr class="SFunction SIndent2 SMarked"><td class=SEntry><a href="#Auth.has_permission" id=link24 onMouseOver="ShowTip(event, 'tt14', 'link24')" onMouseOut="HideTip('tt14')">has_permission()</a></td><td class=SDescription>A convenient shorthand for checking user permissions.</td></tr></table></div></div><!--END_ND_SUMMARY--></div></div></div>

<div class="CGroup"><div class=CTopic><h3 class=CTitle><a name="Auth.Variables"></a>Variables</h3></div></div>

<div class="CVariable"><div class=CTopic><h3 class=CTitle><a name="Auth.$errors"></a>$errors</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public $errors</td></tr></table></blockquote><p>An array of errors generated.</p></div></div></div>

<div class="CVariable"><div class=CTopic><h3 class=CTitle><a name="Auth.$logged_in"></a>$logged_in</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $logged_in</td></tr></table></blockquote><p>Stores the logged in value after the first test to improve performance.</p><h4 class=CHeading>Access</h4><p>Private</p></div></div></div>

<div class="CVariable"><div class=CTopic><h3 class=CTitle><a name="Auth.$ip_address"></a>$ip_address</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $ip_address</td></tr></table></blockquote><p>Stores the ip_address of the current user for performance reasons.</p><h4 class=CHeading>Access</h4><p>Private</p></div></div></div>

<div class="CVariable"><div class=CTopic><h3 class=CTitle><a name="Auth.$perms"></a>$perms</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $perms</td></tr></table></blockquote><p>Stores permissions by role so we don&rsquo;t have to scour the database more than once.</p><h4 class=CHeading>Access</h4><p>Private</p></div></div></div>

<div class="CVariable"><div class=CTopic><h3 class=CTitle><a name="Auth.$ci"></a>$ci</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $ci</td></tr></table></blockquote><p>A pointer to the CodeIgniter instance.</p><h4 class=CHeading>Access</h4><p>Private</p></div></div></div>

<div class="CGroup"><div class=CTopic><h3 class=CTitle><a name="Auth.Functions"></a>Functions</h3></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.__construct"></a>__construct()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function __construct()</td></tr></table></blockquote><p>Grabs a pointer to the CI instance, gets the user&rsquo;s IP address, and attempts to automatically log in the user.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.login"></a>login()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function login(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$password</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$remember</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>false</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Attempt to log the user in.</p><h4 class=CHeading>Parameters</h4><p>$login      The user&rsquo;s login credentials (email/username) $password   The user&rsquo;s password $remember   Whether the user should be remembered in the system.</p><h4 class=CHeading>Return</h4><p>true/false on success/failure.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.logout"></a>logout()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function logout()</td></tr></table></blockquote><p>Destroys the autologin information and the current session.</p><h4 class=CHeading>Return</h4><p>void</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.is_logged_in"></a>is_logged_in()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function is_logged_in()</td></tr></table></blockquote><p>Checks the session for the required info, then verifies against the database.</p><h4 class=CHeading>Return</h4><p>true/false</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.restrict"></a>restrict()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function restrict(</td><td class=PParameter nowrap>$permission</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Checks that a user is logged in (and, optionally of the correct role) and, if not, send them to the login screen.</p><p>If no permission is checked, will simply verify that the user is logged in.&nbsp; If a permission is passed in to the first parameter, will check the user&rsquo;s role and verify that role has the appropriate permission.</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$permission</td><td class=CDLDescription>(Optional) A string representing the permission to check for.</td></tr></table><h4 class=CHeading>Return</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>true</td><td class=CDLDescription>if the user has the appropriate access permissions.</td></tr><tr><td class=CDLEntry>redirect</td><td class=CDLDescription>to the previous page if the user doesn&rsquo;t have permissions.</td></tr><tr><td class=CDLEntry>redirect</td><td class=CDLDescription>&rsquo;/login&rsquo; page if the user is not logged in.</td></tr></table></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.user_id"></a>user_id()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function user_id()</td></tr></table></blockquote><p>Retrieves the user_id from the current session.</p><h4 class=CHeading>Return</h4><p>The user&rsquo;s id.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.email"></a>email()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function email()</td></tr></table></blockquote><p>Retrieves the email address from the current session.</p><h4 class=CHeading>Return</h4><p>The user&rsquo;s email.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.role_id"></a>role_id()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function role_id()</td></tr></table></blockquote><p>Retrieves the role_id from the current session.</p><h4 class=CHeading>Return</h4><p>The user&rsquo;s role_id.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.has_permission"></a>has_permission()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function has_permission(</td><td class=PParameter nowrap>$permission</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%> null,</td></tr><tr><td></td><td class=PParameter nowrap>$role_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Verifies that the user is logged in and has the appropriate access permissions.</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$permission</td><td class=CDLDescription>A string with the permission to check for, ie &lsquo;Site.Signin.Allow&rsquo;</td></tr><tr><td class=CDLEntry>$role_id</td><td class=CDLDescription>The id of the role to check the permission against.&nbsp; If not role_id is passed into the method, then it assumes it to be the current user&rsquo;s role_id.</td></tr></table><h4 class=CHeading>Return</h4><p>true/false</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.role_name_by_id"></a>role_name_by_id()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function role_name_by_id(</td><td class=PParameter nowrap>$role_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Retrieves the role_name for the request role.</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$role_id</td><td class=CDLDescription>An int representing the role_id.</td></tr></table><h4 class=CHeading>Return</h4><p>A string with the name of the matched role.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.increase_login_attempts"></a>increase_login_attempts()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>protected function increase_login_attempts(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Records a login attempt into the database.</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$login</td><td class=CDLDescription>The login id used (typically email or username)</td></tr></table><h4 class=CHeading>Return</h4><p>void</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.clear_login_attempts"></a>clear_login_attempts()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>protected function clear_login_attempts(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$expires</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%> 86400</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Clears all login attempts for this user, as well as cleans out old logins.</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$login</td><td class=CDLDescription>The login credentials (typically email)</td></tr><tr><td class=CDLEntry>$expires</td><td class=CDLDescription>The time (in seconds) that attempts older than will be deleted</td></tr></table><h4 class=CHeading>Return</h4><p>void</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.num_login_attempts"></a>num_login_attempts()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>function num_login_attempts(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Get number of attempts to login occured from given IP-address and/or login</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$login</td><td class=CDLDescription>(Optional) The login id to check for (email/username).&nbsp; If no login is passed in, it will only check against the IP Address of the current user.</td></tr></table><h4 class=CHeading>Return</h4><p>An int with the number of attempts.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.autologin"></a>autologin()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private function autologin()</td></tr></table></blockquote><p>Attempts to log the user in based on an existing &lsquo;autologin&rsquo; cookie.</p><h4 class=CHeading>Return</h4><p>void</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.create_autologin"></a>create_autologin()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>private function create_autologin(</td><td class=PParameter nowrap>$user_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>0,</td></tr><tr><td></td><td class=PParameter nowrap>$old_token</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Create the auto-login entry in the database.&nbsp; This method uses Charles Miller&rsquo;s thoughts at: <a href="http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice/" class=LURL target=_top>http://fishbowl.pastiche.org<wbr>/2004<wbr>/01<wbr>/19<wbr>/persistent_login_cookie_best_practice<wbr>/</a></p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$user_id</td><td class=CDLDescription>An int representing the user_id.</td></tr><tr><td class=CDLEntry>$old_token</td><td class=CDLDescription>The previous token that was used to login with.</td></tr></table><h4 class=CHeading>Return</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>true/false</td><td class=CDLDescription>Whether the autologin was created or not.</td></tr></table><h4 class=CHeading>Access</h4><p>Private</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.delete_autologin"></a>delete_autologin()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private function delete_autologin()</td></tr></table></blockquote><p>Deletes the autologin cookie for the current user.</p><h4 class=CHeading>Return</h4><p>void</p><h4 class=CHeading>Access</h4><p>Private</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.setup_session"></a>setup_session()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>private function setup_session(</td><td class=PParameter nowrap>$user_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>0,</td></tr><tr><td></td><td class=PParameter nowrap>$password_hash</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$email</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>'',</td></tr><tr><td></td><td class=PParameter nowrap>$role_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>0,</td></tr><tr><td></td><td class=PParameter nowrap>$remember</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>false,</td></tr><tr><td></td><td class=PParameter nowrap>$old_token</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>Creates the session information for the current user.&nbsp; Will also create an autologin cookie if required.</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$user_id</td><td class=CDLDescription>An int with the user&rsquo;s id</td></tr><tr><td class=CDLEntry>$password_hash</td><td class=CDLDescription>The user&rsquo;s password hash.&nbsp; Used to create a new, unique user_token.</td></tr><tr><td class=CDLEntry>$email</td><td class=CDLDescription>The user&rsquo;s email address.</td></tr><tr><td class=CDLEntry>$role_id</td><td class=CDLDescription>The user&rsquo;s role_id</td></tr><tr><td class=CDLEntry>$remember</td><td class=CDLDescription>A boolean (true/false).&nbsp; Whether to keep the user logged in.</td></tr></table><h4 class=CHeading>Return</h4><p>true/false on success/failure.</p><h4 class=CHeading>Access</h4><p>Private</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.auth_errors"></a>auth_errors()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>if (</td><td class=PParameter nowrap>!function_exists('auth_errors')</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>A utility function for showing authentication errors.</p><h4 class=CHeading>Return</h4><p>A string with a &lt;ul&gt; tag of any auth errors, or an empty string if no errors exist.</p></div></div></div>

<div class="CFunction"><div class=CTopic><h3 class=CTitle><a name="Auth.has_permission"></a>has_permission()</h3><div class=CBody><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>function has_permission(</td><td class=PParameter nowrap>$permission</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote><p>A convenient shorthand for checking user permissions.</p><h4 class=CHeading>Parameters</h4><table border=0 cellspacing=0 cellpadding=0 class=CDescriptionList><tr><td class=CDLEntry>$permission</td><td class=CDLDescription>The permission to check for, ie &lsquo;Site.Signin.Allow&rsquo;</td></tr></table><h4 class=CHeading>Return</h4><p>true/false</p></div></div></div>

</div><!--Content-->


<div id=Footer><a href="http://www.naturaldocs.org">Generated by Natural Docs</a></div><!--Footer-->


<div id=Menu><div class=MTitle>Bonfire<div class=MSubTitle>Faster CodeIgniter Development</div></div><div class=MEntry><div class=MGroup><a href="javascript:ToggleMenu('MGroupContent1')">Guides</a><div class=MGroupContent id=MGroupContent1><div class=MEntry><div class=MFile><a href="../../../../files2/installing-txt.html">Installing Bonfire</a></div></div><div class=MEntry><div class=MFile><a href="../../../../files2/modules/overview-txt.html">Modules Overview</a></div></div><div class=MEntry><div class=MFile><a href="../../../../files2/auth_notes-txt.html">Understanding Authentication</a></div></div></div></div></div><div class=MEntry><div class=MGroup><a href="javascript:ToggleMenu('MGroupContent2')">Helpers</a><div class=MGroupContent id=MGroupContent2><div class=MEntry><div class=MFile><a href="../../../helpers/address_helper-php.html">Address Helper</a></div></div><div class=MEntry><div class=MFile><a href="../../../helpers/application_helper-php.html">Application Helper</a></div></div><div class=MEntry><div class=MFile><a href="../../../helpers/config_file_helper-php.html">Config File Helper</a></div></div></div></div></div><div class=MEntry><div class=MGroup><a href="javascript:ToggleMenu('MGroupContent3')">Libraries</a><div class=MGroupContent id=MGroupContent3><div class=MEntry><div class=MFile><a href="../../../libraries/assets-php.html">Assets</a></div></div><div class=MEntry><div class=MFile><a href="../../../libraries/template-php.html">Template</a></div></div><div class=MEntry><div class=MFile id=MSelected>Auth</div></div><div class=MEntry><div class=MFile><a href="../../../libraries/Console-php.html">Console</a></div></div></div></div></div><div class=MEntry><div class=MGroup><a href="javascript:ToggleMenu('MGroupContent4')">Core Overrides</a><div class=MGroupContent id=MGroupContent4><div class=MEntry><div class=MFile><a href="../../../core/MY_Controller-php.html">Controllers</a></div></div><div class=MEntry><div class=MFile><a href="../../../core/MY_Model-php.html">MY_Model</a></div></div></div></div></div><div class=MEntry><div class=MGroup><a href="javascript:ToggleMenu('MGroupContent5')">Core Modules</a><div class=MGroupContent id=MGroupContent5><div class=MEntry><div class=MFile><a href="../../emailer/libraries/emailer-php.html">Emailer</a></div></div><div class=MEntry><div class=MFile><a href="../../migrations/libraries/Migrations-php.html">Migrations</a></div></div><div class=MEntry><div class=MFile><a href="../../roles/models/permission_model-php.html">Permission_model</a></div></div><div class=MEntry><div class=MFile><a href="../../roles/models/role_model-php.html">Role_model</a></div></div></div></div></div><div class=MEntry><div class=MFile><a href="../../../controllers/install-php.html">controllers/<wbr>install.php</a></div></div><div class=MEntry><div class=MFile><a href="../../logs/controllers/developer-php.html">core_modules/.../controllers/<wbr>developer.php</a></div></div><div class=MEntry><div class=MGroup><a href="javascript:ToggleMenu('MGroupContent6')">Index</a><div class=MGroupContent id=MGroupContent6><div class=MEntry><div class=MIndex><a href="../../../../index/General.html">Everything</a></div></div><div class=MEntry><div class=MIndex><a href="../../../../index/Classes.html">Classes</a></div></div><div class=MEntry><div class=MIndex><a href="../../../../index/Functions.html">Functions</a></div></div><div class=MEntry><div class=MIndex><a href="../../../../index/Variables.html">Variables</a></div></div><div class=MEntry><div class=MIndex><a href="../../../../index/Files.html">Files</a></div></div></div></div></div><script type="text/javascript"><!--
var searchPanel = new SearchPanel("searchPanel", "HTML", "../../../../search");
--></script><div id=MSearchPanel class=MSearchPanelInactive><input type=text id=MSearchField value=Search onFocus="searchPanel.OnSearchFieldFocus(true)" onBlur="searchPanel.OnSearchFieldFocus(false)" onKeyUp="searchPanel.OnSearchFieldChange()"><select id=MSearchType onFocus="searchPanel.OnSearchTypeFocus(true)" onBlur="searchPanel.OnSearchTypeFocus(false)" onChange="searchPanel.OnSearchTypeChange()"><option  id=MSearchEverything selected value="General">Everything</option><option value="Classes">Classes</option><option value="Files">Files</option><option value="Functions">Functions</option><option value="Variables">Variables</option></select></div><script language=JavaScript><!--
HideAllBut([3, 4, 2, 5, 1], 7);// --></script></div><!--Menu-->



<!--START_ND_TOOLTIPS-->
<div class=CToolTip id="tt1"><div class=CVariable><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public $errors</td></tr></table></blockquote>An array of errors generated.</div></div><div class=CToolTip id="tt2"><div class=CVariable><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $logged_in</td></tr></table></blockquote>Stores the logged in value after the first test to improve performance.</div></div><div class=CToolTip id="tt3"><div class=CVariable><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $ip_address</td></tr></table></blockquote>Stores the ip_address of the current user for performance reasons.</div></div><div class=CToolTip id="tt4"><div class=CVariable><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $perms</td></tr></table></blockquote>Stores permissions by role so we don&rsquo;t have to scour the database more than once.</div></div><div class=CToolTip id="tt5"><div class=CVariable><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private $ci</td></tr></table></blockquote>A pointer to the CodeIgniter instance.</div></div><div class=CToolTip id="tt6"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function __construct()</td></tr></table></blockquote>Grabs a pointer to the CI instance, gets the user&rsquo;s IP address, and attempts to automatically log in the user.</div></div><div class=CToolTip id="tt7"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function login(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$password</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$remember</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>false</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Attempt to log the user in.</div></div><div class=CToolTip id="tt8"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function logout()</td></tr></table></blockquote>Destroys the autologin information and the current session.</div></div><div class=CToolTip id="tt9"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function is_logged_in()</td></tr></table></blockquote>Checks the session for the required info, then verifies against the database.</div></div><div class=CToolTip id="tt10"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function restrict(</td><td class=PParameter nowrap>$permission</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Checks that a user is logged in (and, optionally of the correct role) and, if not, send them to the login screen.</div></div><div class=CToolTip id="tt11"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function user_id()</td></tr></table></blockquote>Retrieves the user_id from the current session.</div></div><div class=CToolTip id="tt12"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function email()</td></tr></table></blockquote>Retrieves the email address from the current session.</div></div><div class=CToolTip id="tt13"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>public function role_id()</td></tr></table></blockquote>Retrieves the role_id from the current session.</div></div><div class=CToolTip id="tt14"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function has_permission(</td><td class=PParameter nowrap>$permission</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%> null,</td></tr><tr><td></td><td class=PParameter nowrap>$role_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Verifies that the user is logged in and has the appropriate access permissions.</div></div><div class=CToolTip id="tt15"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>public function role_name_by_id(</td><td class=PParameter nowrap>$role_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Retrieves the role_name for the request role.</div></div><div class=CToolTip id="tt16"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>protected function increase_login_attempts(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Records a login attempt into the database.</div></div><div class=CToolTip id="tt17"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>protected function clear_login_attempts(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$expires</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%> 86400</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Clears all login attempts for this user, as well as cleans out old logins.</div></div><div class=CToolTip id="tt18"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>function num_login_attempts(</td><td class=PParameter nowrap>$login</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Get number of attempts to login occured from given IP-address and/or login</div></div><div class=CToolTip id="tt19"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private function autologin()</td></tr></table></blockquote>Attempts to log the user in based on an existing &lsquo;autologin&rsquo; cookie.</div></div><div class=CToolTip id="tt20"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>private function create_autologin(</td><td class=PParameter nowrap>$user_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>0,</td></tr><tr><td></td><td class=PParameter nowrap>$old_token</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Create the auto-login entry in the database. </div></div><div class=CToolTip id="tt21"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td>private function delete_autologin()</td></tr></table></blockquote>Deletes the autologin cookie for the current user.</div></div><div class=CToolTip id="tt22"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>private function setup_session(</td><td class=PParameter nowrap>$user_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>0,</td></tr><tr><td></td><td class=PParameter nowrap>$password_hash</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null,</td></tr><tr><td></td><td class=PParameter nowrap>$email</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>'',</td></tr><tr><td></td><td class=PParameter nowrap>$role_id</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>0,</td></tr><tr><td></td><td class=PParameter nowrap>$remember</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>false,</td></tr><tr><td></td><td class=PParameter nowrap>$old_token</td><td class=PDefaultValuePrefix>&nbsp;=&nbsp;</td><td class=PDefaultValue width=100%>null</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>Creates the session information for the current user. </div></div><div class=CToolTip id="tt23"><div class=CFunction><blockquote><table border=0 cellspacing=0 cellpadding=0 class="Prototype prettyprint"><tr><td><table border=0 cellspacing=0 cellpadding=0><tr><td class=PBeforeParameters nowrap>if (</td><td class=PParameter nowrap>!function_exists('auth_errors')</td><td class=PAfterParameters nowrap>)</td></tr></table></td></tr></table></blockquote>A utility function for showing authentication errors.</div></div><!--END_ND_TOOLTIPS-->




<div id=MSearchResultsWindow><iframe src="" frameborder=0 name=MSearchResults id=MSearchResults></iframe><a href="javascript:searchPanel.CloseResultsWindow()" id=MSearchResultsWindowClose>Close</a></div>


<script language=JavaScript><!--
if (browserType) {if (browserVer) {document.write("</div>"); }document.write("</div>");}// --></script></body></html>